package com.gy.emailservice.service.impl;

import com.gy.emailservice.entity.CopyPOJO;
import com.gy.emailservice.entity.EmailEntity;
import com.gy.emailservice.entity.ParamDTO;
import com.gy.emailservice.mapper.CommonMapper;
import com.gy.emailservice.service.EmailSendService;
import com.gy.emailservice.webservice.EmailServicePortType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author jayson
 * @version 2.0
 * @date 2021-07-07 16:43
 */
@Service
public class EmailSendServiceImpl implements EmailSendService {

    @Resource
    private CommonMapper commonMapper;
    @Autowired
    private EmailServicePortType emailServicePortType;

    /**
     * {
     * "moduleId":1,
     * "sendEmpNo":"GY004163",
     * "subject":"客户应回款报表",
     * "content":"暂无内容",
     * "dateDir":"2021-07-07"
     * }
     *
     * @param paramDTO
     * @return
     */
    @Override
    public List<String> sendEmail(ParamDTO paramDTO) {

        System.out.println("时间：" + new Date() + ",参数：" + paramDTO.toString());

        List<EmailEntity> eMailInfo = commonMapper.getEMailInfo(paramDTO.getDateDir(), paramDTO.getModuleId());

        List<String> results = new ArrayList<>();
        eMailInfo.forEach(emailEntity -> {

            String title = paramDTO.getSubject();
            String content = paramDTO.getContent();
            if (title == null) {

                title = emailEntity.getModuleName();

            }

            if (content == null) {

                content = "";

            }

            try {
                String s = emailServicePortType.sendInternalMail(
                        paramDTO.getSendEmpNo(),
                        emailEntity.getToEmpNo(),
                        emailEntity.getCcEmpNos(),
                        emailEntity.getBccEmpNos(),
                        title,
                        content,
                        emailEntity.getFilePath());
                results.add(s);
            } catch (RemoteException e) {
                e.printStackTrace();
            }

        });

        return results;
    }

    @Override
    public Integer copy(CopyPOJO copyPOJO) {
        copyFolder(copyPOJO.getOldPath(), copyPOJO.getNewPath());
        return 1;
    }

    /**
     * 复制整个文件夹内容
     *
     * @param oldPath String 原文件路径 如：c:/fqf
     * @param newPath String 复制后路径 如：f:/fqf/ff
     * @return boolean
     */
    public void copyFolder(String oldPath, String newPath) {

        try {
            (new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹
            File a = new File(oldPath);
            String[] file = a.list();
            File temp = null;
            for (int i = 0; i < file.length; i++) {
                if (oldPath.endsWith(File.separator)) {
                    temp = new File(oldPath + file[i]);
                } else {
                    temp = new File(oldPath + File.separator + file[i]);
                }

                if (temp.isFile()) {
                    FileInputStream input = new FileInputStream(temp);
                    FileOutputStream output = new FileOutputStream(newPath + "/" +
                            (temp.getName()).toString());
                    byte[] b = new byte[1024 * 5];
                    int len;
                    while ((len = input.read(b)) != -1) {
                        output.write(b, 0, len);
                    }
                    output.flush();
                    output.close();
                    input.close();
                }
                if (temp.isDirectory()) {//如果是子文件夹
                    copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
                }
            }
        } catch (Exception e) {
            System.out.println("复制整个文件夹内容操作出错");
            e.printStackTrace();

        }

    }
}
